package com.example.userregist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;

public class ShopActivity extends AppCompatActivity{

    private Button button1;
    private Button button2;
    private Button button3;
    private ItemInfo itemInfo1;
    private ItemInfo itemInfo2;
    private ItemInfo itemInfo3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        itemInfo1 = new ItemInfo("大宝剑",20,50,20);
        itemInfo2 = new ItemInfo("无敌盾",50,20,20);
        itemInfo3 = new ItemInfo("攻防一体",25,25,20);

        TextView name1 = findViewById(R.id.tv_name);
        TextView life1 = findViewById(R.id.tv_life);
        TextView attack1 = findViewById(R.id.tv_attack);
        TextView speed1 = findViewById(R.id.tv_speed);

        TextView name2 = findViewById(R.id.tv_name2);
        TextView life2 = findViewById(R.id.tv_life2);
        TextView attack2 = findViewById(R.id.tv_attack2);
        TextView speed2 = findViewById(R.id.tv_speed2);
        TextView name3 = findViewById(R.id.tv_name3);
        TextView life3 = findViewById(R.id.tv_life3);
        TextView attack3 = findViewById(R.id.tv_attack3);
        TextView speed3 = findViewById(R.id.tv_speed3);
        //设置值
        name1.setText("商品名称"+itemInfo1.getName());
        life1.setText("生命值"+itemInfo1.getHp());
        attack1.setText("攻击力"+itemInfo1.getAttack());
        speed1.setText("速度"+itemInfo1.getSpeed());

        name2.setText("商品名称"+itemInfo2.getName());
        life2.setText("生命值"+itemInfo2.getHp());
        attack2.setText("攻击力"+itemInfo2.getAttack());
        speed2.setText("速度"+itemInfo2.getSpeed());

        name3.setText("商品名称"+itemInfo3.getName());
        life3.setText("生命值"+itemInfo3.getHp());
        attack3.setText("攻击力"+itemInfo3.getAttack());
        speed3.setText("速度"+itemInfo3.getSpeed());

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWeaponResult(itemInfo1);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWeaponResult(itemInfo2);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWeaponResult(itemInfo3);
            }
        });
    }
    //购买
    private void setWeaponResult(ItemInfo itemInfo)
    {
        Intent intent=new Intent();
        //intent.putExtra("ItemInfo",itemInfo);
        Gson gson = new Gson();
        String itemInfoStr = gson.toJson(itemInfo);
        intent.putExtra("ItemInfo",itemInfoStr);
        setResult(RESULT_OK,intent);
        finish();
    }
}
